import { LevelConfig } from "../../Data/LevelConfig";
import { StaticData } from "../../Data/StaticData";
import HttpAPI from "../../HttpManager/HttpAPI";
import HttpManager from "../../HttpManager/HttpManager";
import { EViewType } from "../Data/DataType";
import { EventNames } from "../Data/EventNames";
import { LevelItemInfo } from "../Data/LevelItemInfo";
import { HomePageRunBase } from "./HomePageRun.generated";
import { LevelItem } from "./LevelItem";

const { regClass, property } = Laya;

@regClass()
export class HomePageRun extends HomePageRunBase {
    /**关卡列表信息 */
    private arrLevelItemInfo: LevelItemInfo[] = [];

    onAwake(): void {
        this.init();
        this.addEventListener();
    }

    onDestroy(): void {
        this.removeEventListener();
    }

    private addEventListener(): void {
        this.btnExit.on(Laya.Event.CLICK, this, this.onClickExitBtn);
        this.btnSetting.on(Laya.Event.CLICK, this, this.onClickSettingBtn);
        this.btnShop.on(Laya.Event.CLICK, this, this.onClickShopBtn);
        this.btnDaily.on(Laya.Event.CLICK, this, this.onClickDailyBtn);
        this.btnRank.on(Laya.Event.CLICK, this, this.onClickRankBtn);
        Laya.stage.on(EventNames.GAME_GO_HOME, this, this.eventGameGoHome);
        Laya.stage.on(EventNames.INTEGRAL_CHANGE,this,this.modifyScore)
    }

    private removeEventListener(): void {
        this.btnExit.off(Laya.Event.CLICK, this, this.onClickExitBtn);
        this.btnSetting.off(Laya.Event.CLICK, this, this.onClickSettingBtn);
        this.btnShop.off(Laya.Event.CLICK, this, this.onClickShopBtn);
        this.btnDaily.off(Laya.Event.CLICK, this, this.onClickDailyBtn);
        this.btnRank.off(Laya.Event.CLICK, this, this.onClickRankBtn);
        Laya.stage.off(EventNames.GAME_GO_HOME, this, this.eventGameGoHome)
        Laya.stage.off(EventNames.INTEGRAL_CHANGE,this,this.modifyScore)
    }

    private init() {
        this.initUserInfo();
        this.initLevelItemListData();
        this.arrLevelItemInfo = []
        this.updateLevelItemList();
        this.getHomeInfo();
    }
    /**监听点击退出按钮 */
    private onClickExitBtn(): void {
        console.log("点击退出按钮");
        // Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
    }
    /**监听点击设置按钮 */
    private onClickSettingBtn(): void {
        console.log("点击设置按钮");
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        Laya.stage.event(EventNames.GAME_OPEN_VIEW, EViewType.Setting);
    }
    /**监听点击商店按钮 */
    private onClickShopBtn(): void {
        console.log("点击商店按钮");
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        Laya.stage.event(EventNames.GAME_OPEN_VIEW, EViewType.Shop);
    }
    /**监听点击日报按钮 */
    private onClickDailyBtn(): void {
        console.log("点击日报按钮");
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        Laya.stage.event(EventNames.GAME_OPEN_VIEW, EViewType.Daily);
    }
    /**监听点击排行榜按钮 */
    private onClickRankBtn(): void {
        console.log("点击排行榜按钮");
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        Laya.stage.event(EventNames.GAME_OPEN_VIEW, EViewType.Rank);
    }
    /**初始化用户信息 */
    private initUserInfo(): void {
        console.log("initUserInfo=>", StaticData.headUrl);

        if (StaticData.headUrl && StaticData.headUrl != "") {
            Laya.loader.load(StaticData.headUrl, Laya.Handler.create(this, this.onHeadImageLoaded), null, Laya.Loader.IMAGE);
        }
        this.labelName.text = StaticData.nickName;
        let phone = StaticData.phoneNumber;
        if (phone != undefined || phone != null) {
            phone = phone.substring(0, 3) + "****" + phone.substring(7);
        }
        this.labelPhone.text = phone;
        this.modifyScore(StaticData.integral)
    }
    private onHeadImageLoaded(res: any): void {
        this.imgDefaultHead.skin = undefined;
        this.imgDefaultHead.source = res;
        this.imgDefaultHead.width = 100;
        this.imgDefaultHead.height = 100;
    }
    /**创建list列表 */
    private initLevelItemListData(): void {
        this.levelItemList.renderHandler = Laya.Handler.create(this, this.onListRender, undefined, false);
        this.levelItemList.array = this.arrLevelItemInfo;
    }
    /**修改积分值 */
    private modifyScore(score: number): void {
        this.labelScore.text =score.toString();
    }

    /**监听列表滑动，刷新列表里的数据 */
    private onListRender(item: Laya.Box, index: number) {
        item.getComponent(LevelItem).updateInfo(this.arrLevelItemInfo[index]);
    }
    /**刷新关卡列表 */
    private updateLevelItemList(): void {
        // 显示空态
        this.BoxNullPanel.visible = this.arrLevelItemInfo.length <= 0;

        this.levelItemList.array = this.arrLevelItemInfo;
        let count = Math.ceil(this.arrLevelItemInfo.length / 3);
        this.levelItemList.repeatY = count;
        this.levelItemList.refresh();
        this.levelItemList.scrollTo(0);
    }

    private eventGameGoHome() {
        this.getHomeInfo();
    }

    private getHomeInfo() {
        HttpManager.get(HttpAPI.GET_HOME_INFO, null, null).then(this.callbackGetHomeInfo.bind(this)).catch(this.callbackGetHomeInfo.bind(this));
    }

    private callbackGetHomeInfo(data: any) {
        this.arrLevelItemInfo = [];
        LevelConfig.arrOflevelId = [];

        if (data && data.success) {
            const result = data.result;
            StaticData.integral=result.integral;
            console.log(StaticData.integral);    
            for (let item of result.gameItems) {
                const data = {
                    levelId: item.barrierId,
                    passLevelState: item.answerStatus == 1,
                    levelIconSrc: item.barrierImgUrl,
                    levelName: item.barrierName,
                }
                let info: LevelItemInfo = new LevelItemInfo(data);
                this.arrLevelItemInfo.push(info);
                LevelConfig.arrOflevelId.push(info.levelId)
            }
            this.modifyScore(StaticData.integral)
            console.log("关卡ID数据", LevelConfig.arrOflevelId);
        }


        this.updateLevelItemList();

    }

}